/*
 This file 'flop' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2020 Lincong

 Contact:  
        Email: lincong1987@gmail.com

        QQ: 159257119
 
 See Usage at http://www.jplatformx.com/firebird

 Create date: 2020-07-05 18:06
 */


define(function (require, exports, module) {


	(function () {
		var lastTime = 0;
		var vendors = ['webkit', 'moz'];
		for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
			window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
			window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||    // Webkit中此取消方法的名字变了
				window[vendors[x] + 'CancelRequestAnimationFrame'];
		}

		if (!window.requestAnimationFrame) {
			window.requestAnimationFrame = function (callback, element) {
				var currTime = new Date().getTime();
				var timeToCall = Math.max(0, 16.7 - (currTime - lastTime));
				var id = window.setTimeout(function () {
					callback(currTime + timeToCall);
				}, timeToCall);
				lastTime = currTime + timeToCall;
				return id;
			};
		}
		if (!window.cancelAnimationFrame) {
			window.cancelAnimationFrame = function (id) {
				clearTimeout(id);
			};
		}
	}());

	var pad = function (source, length, pad) {
		var pre = "", negative = (source < 0), string = String(Math.abs(source)),
			pad = typeof pad === "undefined" ? "0" : pad;
		if (string.length < length) {
			pre = (new Array(length - string.length + 1)).join(pad);
		}

		return (negative ? "-" : "") + pre + string;
	};

	var renderSpans = function (num, opts) {
		var length = num.length;

		var numString = opts.prefix + (pad(num, opts.maxLength, opts.pad) + "") + opts.suffix

		var $spans = [];
		$.each(numString.split(""), function (i, n) {
			$spans.push("<span>" + n + "</span>");
		});

		return $spans.join("");

	};

	var renderText = function (num, opts) {
		var length = num.length;

		var numString = opts.prefix + (pad(num, opts.maxLength, opts.pad) + "") + opts.suffix

		var texts = [];
		$.each(numString.split(""), function (i, n) {
			texts.push(n);
		});

		return texts.join("");
	};

	/**
	 * 翻牌器
	 *
	 * @type {function(*=): flop}
	 *
	 * @param options {
	 *     speed: 24,
				value: 0,
				maxLength: -1,
				pad: "0",
				prefix: "",
				suffix: ""
	 *
	 * }
	 *
	 */
	var flop = (function ($) {

		var flop = function (options) {
			var defaults = {
				speed: 24,
				value: 0,
				start: 0,
				maxLength: -1,
				pad: "0",
				prefix: "",
				suffix: "",
				rich: true
			};
			var opts = $.extend(true, {}, defaults, options);

			var div_by = 100, value = opts["value"], speed = Math.floor(value / div_by), sum = opts.start, $display = $(this),
				currentValue = 1, int_speed = opts["speed"];

			var timer;
			var fn = function () {
				if (currentValue <= div_by && speed != 0) {
					sum = speed * currentValue;
					opts.rich === true ? $display.html(renderSpans(sum, opts)) : $display.text(renderText(sum, opts));
					currentValue++;
					timer = requestAnimationFrame(function () {
						fn();
					}, int_speed);
				} else if (sum < value) {
					opts.rich === true ? $display.html(renderSpans(++sum, opts)) : $display.text(
						renderText(++sum, opts));
					timer = requestAnimationFrame(function () {
						fn();
					}, int_speed);
				} else {
					cancelAnimationFrame(timer);
				}
			};

			timer = requestAnimationFrame(function () {
				fn();
			}, int_speed);

			return this;
		}


		$.fn.flop = function (options) {
			return $(this).each(function () {
				return flop.apply(this, [options]);
			})
		};


		return flop;
	})(jQuery);


	module.exports = flop;

});